<?php

class Gallery_model extends MY_Model
{
    private $module = 'gallery';
    private $table = 'gallery_category';

    function getsearchContent($limit, $page)
    {
        $this->db->select('*');
        $this->db->limit($limit, $page);
        $this->db->order_by($this->input->post('func_order_by'), $this->input->post('order_by'));
        if ($this->input->post('content') != '' && $this->input->post('content') != 'type here...') {
            $this->db->where('(`name` LIKE "%' . $this->input->post('content') . '%")');
        }
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') == '') {
            $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
        }
        if ($this->input->post('dateFrom') == '' && $this->input->post('dateTo') != '') {
            $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') != '') {
            $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
            $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        $query = $this->db->get(PREFIX . $this->table);

        if ($query->result()) {
            return $query->result();
        } else {
            return false;
        }
    }

    function getTotalsearchContent()
    {
        $this->db->select('*');
        if ($this->input->post('content') != '' && $this->input->post('content') != 'type here...') {
            $this->db->where('(`name` LIKE "%' . $this->input->post('content') . '%")');
        }
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') == '') {
            $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
        }
        if ($this->input->post('dateFrom') == '' && $this->input->post('dateTo') != '') {
            $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') != '') {
            $this->db->where('created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
            $this->db->where('created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        $query = $this->db->count_all_results(PREFIX . $this->table);

        if ($query > 0) {
            return $query;
        } else {
            return false;
        }
    }

    function getDetailManagement($id)
    {
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get(PREFIX . $this->table);

        if ($query->result()) {
            return $query->result();
        } else {
            return false;
        }
    }

    function saveManagement($fileName = '')
    {
        if ($this->input->post('statusAdmincp') == 'on') {
            $status = 1;
        } else {
            $status = 0;
        }

        if ($this->input->post('hiddenIdAdmincp') == 0) {
            //Kiểm tra đã tồn tại chưa?
            $checkData = $this->checkData($this->input->post('nameAdmincp'));
            if ($checkData) {
                print 'error-name-exists';
                exit;
            }
            $data = array(
                'name' => $this->input->post('nameAdmincp'),
                'link' => $this->input->post('link'),
                'type' => $this->input->post('type'),
                'image' => $fileName['image'],
                'status' => $status,
                'created' => date('Y-m-d H:i:s', time())
            );
            if ($this->db->insert(PREFIX . $this->table, $data)) {
                modules::run('admincp/saveLog', 'gallery_category', $this->db->insert_id(), 'Add new', 'Add new');
                return true;
            }
        } else {
            $result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
            //Kiểm tra đã tồn tại chưa?
            if ($result[0]->name != $this->input->post('nameAdmincp')) {
                $checkData = $this->checkData($this->input->post('nameAdmincp'));
                if ($checkData) {
                    print 'error-name-exists';
                    exit;
                }
            }

            //Xử lý xóa hình khi update thay đổi hình
            if ($fileName['image'] == '') {
                $fileName['image'] = $result[0]->image;
            } else {
                @unlink(BASEFOLDER . DIR_UPLOAD_GALLERY_CATEGORY . $result[0]->image);
            }

            $data = array(
                'name' => $this->input->post('nameAdmincp'),
                'link' => $this->input->post('link'),
                'type' => $this->input->post('type'),
                'image' => $fileName['image'],
                'status' => $status
            );
            modules::run('admincp/saveLog', 'gallery_category', $this->input->post('hiddenIdAdmincp'), '', 'Update', $result, $data);
            $this->db->where('id', $this->input->post('hiddenIdAdmincp'));
            if ($this->db->update(PREFIX . $this->table, $data)) {
                return true;
            }
        }
        return false;
    }

    function getListImageOfGallery($category_id)
    {
        $this->db->select('id,image');
        $this->db->where('category_id', $category_id);
        $this->db->order_by('created', 'DESC');
        $query = $this->db->get(PREFIX . 'gallery');

        if ($query->result()) {
            return $query->result();
        } else {
            return false;
        }
    }

    function getDetailImageOfGallery($id)
    {
        $this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get(PREFIX . 'gallery');

        if ($query->result()) {
            return $query->result();
        } else {
            return false;
        }
    }

    function checkData($name)
    {
        $this->db->select('id');
        $this->db->where('name', $name);
        $this->db->limit(1);
        $query = $this->db->get(PREFIX . $this->table);

        if ($query->result()) {
            return true;
        } else {
            return false;
        }
    }
}